package aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/01/19
 * @description 安全日志类
 */
@Component
@Aspect
public class SecurityLogAspect {
    @Pointcut("execution(* service.ArticleService.save*(..))")
    public void savePointCut() {}
    @Pointcut("execution(* service.ArticleService.update*(..))")
    public void updatePointCut() {}
    @Pointcut("execution(* service.ArticleService.delete*(..))")
    public void deletePointCut() {}
    // 定义多个切点
    @Around("savePointCut() || updatePointCut() || deletePointCut()")
    public void log(ProceedingJoinPoint joinPoint) throws Throwable {
        joinPoint.proceed();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String nowTime = sdf.format(new Date());
        // 用户名可以通过session取
        Signature signature = joinPoint.getSignature();
        // getDeclaringTypeName 全类名
        System.out.println(nowTime + "userName: " + signature.getDeclaringTypeName() + "." + signature.getName());
    }
}
